package forum.server.controller;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @(#)Security.java
 *
 *
 * @author Tomer Heber
 * @version 1.00 2010/3/7
 */
public class Security {
        
	/**
	 * A function that receives a password in String representation and returns
	 * the encryption of the password in hex representation.
	 * 
	 * @param password The password which we want to encrypt
	 * @return the encrypted password in hex representation
	 * @throws NoSuchAlgorithmException if MD5 is not supported by this version of java
	 */
	public static String encryptPassword(String password) throws NoSuchAlgorithmException {
		String encryptedPassword = "";
		
		byte[] b = password.getBytes();
		MessageDigest md = MessageDigest.getInstance("MD5");
		md.update(b);
		b = md.digest();
		
		for (int i = 0; i < b.length; i++) {
			encryptedPassword += String.format("%02x",0xFF & b[i]);
		}

		return encryptedPassword;
	}
}